#include "egl_wrapper.h"    
#include <cstdio>

int eglInit(EGLDisplay &dpy, EGLContext &context) {
  dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  if (dpy == EGL_NO_DISPLAY) {
      printf("eglGetDisplay returned EGL_NO_DISPLAY.\n");
      return -1;
  }

  EGLint majorVersion;
  EGLint minorVersion;
  EGLBoolean returnValue = eglInitialize(dpy, &majorVersion, &minorVersion);
  if (returnValue != EGL_TRUE) {
      printf("eglInitialize failed\n");
      return -1;
  }

  EGLConfig cfg;
  EGLint count;
  EGLint s_configAttribs[] = {
          EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR,
          EGL_NONE };
  if (eglChooseConfig(dpy, s_configAttribs, &cfg, 1, &count) == EGL_FALSE) {
      printf("eglChooseConfig failed\n");
      return -1;
  }

  EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE };
  context = eglCreateContext(dpy, cfg, EGL_NO_CONTEXT, context_attribs);
  if (context == EGL_NO_CONTEXT) {
      printf("eglCreateContext failed\n");
      return -1;
  }
  returnValue = eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, context);
  if (returnValue != EGL_TRUE) {
      printf("eglMakeCurrent failed returned %d\n", returnValue);
      return -1;
  }

  return 0;
}

void eglClose(EGLDisplay &dpy, EGLContext &context) {
  eglDestroyContext(dpy, context);
  eglTerminate(dpy);
}